package org.seckill.TestCglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Created by wangyg 
 * on 2017/4/20 11:37
 */
public class CglibProxy implements MethodInterceptor, org.springframework.cglib.proxy.MethodInterceptor {
	Object target;

	public Object init(Object obj) {
		this.target = obj;
		Enhancer eh = new Enhancer();
		eh.setSuperclass(this.target.getClass());
		//		回调方法
		eh.setCallback(this);
		//		创建代理对象
		return eh.create();
	}

	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
		System.out.println("前置执行");
		methodProxy.invokeSuper(o, objects);
		System.out.println(method.getName());
		System.out.println(o.getClass().getName());
		System.out.println(Arrays.toString(objects));
		System.out.println(methodProxy.getClass().getName());
		System.out.println("后置执行");
		return null;
	}

	public Object intercept(Object o, Method method, Object[] objects,
			org.springframework.cglib.proxy.MethodProxy methodProxy) throws Throwable {
		return null;
	}
}
